package com.hliushi.demo.service.impl;

import com.hliushi.demo.entity.UserEntity;
import com.hliushi.demo.repository.UserEntityRepository;
import com.hliushi.demo.service.LoginService;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

/**
 * @author: hliushi
 * @date: 2022/9/10 22:21
 **/
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private UserEntityRepository userEntityRepository;

    @Override
    public boolean existAccount(String account) {
        return userEntityRepository.findByAccount(account);
    }

    @Override
    public void login(String account, String password) {
        if (!existAccount(account)) {
            throw new RuntimeException("「账号不存在」");
        }
        String enCryptPassword = Md5Crypt.md5Crypt(password.getBytes(StandardCharsets.UTF_8));
        boolean result = userEntityRepository.findByAccountAndPassword(account, enCryptPassword);
        if (!result) {
            throw new RuntimeException("「密码错误」");
        }
    }

    @Override
    public UserEntity loginUnSupport(String account, String password) {
        throw new UnsupportedOperationException("");
    }
}
